-----------------------------------------------------------------------
--  File:       Controller.ads
-- Description: 
-- Controllers is the main logic engine of the system. It handles
-- - Sensor events: determine current train position in order for the system to take appropriate actions.
-- - User inputs: Speed, Direction of travel, Turning Direction, Halting.
-- In order to use Controllers, trains position needs to be initialized, also Debug Windows need to be passed in for extra info
-- Other operations are as described below
-- Authors:     Ha Le, Maria
-----------------------------------------------------------------------
with Projdefs;
use Projdefs;
with Raildefs;
with Dac_Controller;
use Dac_Controller;
with Blocks;
with Unsigned_Types;
use Unsigned_Types;
with Topolog2;
use Topolog2;
with Raildefs;
use Raildefs;
with ProjDefs;
use ProjDefs;
WITH Swindows;
USE Swindows;

package Controllers is

	-- Assuming direction going Forward
	-- Init by passing the sensors where the train will be placed In Front Of
   procedure Init_Trains (
         Init_Pos : in     Train_Init_Position);

	-- The debugging Windows
	-- Used for displaying debugging data to the console controller
	procedure Init_W_Int(Debug_Win : in Debug_Array);
	
	-- Set the travelling direction to Forward (CCW)
	-- Does NOT actually make the train moving
   procedure Start_Train_Forward (
         T_Id : in     Train_Id);

	-- Set the travelling direction to Backward (CW)
	-- Does NOT actually make the train moving		 
   procedure Start_Train_Backward (
         T_Id : in     Train_Id);

	-- Set the position of the Next Turnout in the direction of travel to 
	-- Only works if the train is in a safe distance from the turnout
	-- 		(meaning before reaching the sensor guarding the turnout)
   procedure Start_Train_Direction_Left (
         T_Id : in     Train_Id);

	-- Set the position of the Next Turnout in the direction of travel to Right
	-- Only works if the train is in a safe distance from the turnout
	-- 		(meaning before reaching the sensor guarding the turnout)
   procedure Start_Train_Direction_Right (
         T_Id : in     Train_Id);

	-- Set the position of the Next Turnout in the direction of travel to Centre (Meaning Straight)
	-- Only works if the train is in a safe distance from the turnout
	-- 		(meaning before reaching the sensor guarding the turnout)	 
   procedure Start_Train_Direction_Centre (
        T_Id : in     Train_Id);

	-- Set the train speed to Speed_Value (0..255)
	-- Only effective if the train presumes it not safe to move on (i.e. waiting for a turnout)
    procedure Start_Train_Speed (
            T_Id : in     Train_Id;
            Speed_Value   : in Unsigned_8);
    
    -- Increase the speed of the train by a prescribed ammount as defined in Projdefs.ads
	-- Only effective if the train presumes it not safe to move on (i.e. waiting for a turnout)
   procedure Start_Fast (
         T_Id : in     Train_Id);
		 
		 
	-- Reduce the speed of the train by a prescribed ammount as defined in Projdefs.ads
	 procedure Start_Slow (
			 T_Id : in     Train_Id);
			 
	-- Stopp the train immediately. However skidding might occur
	-- After being stopped by this command, the train guarantee won't move automatically again even in Unmanned mode until instructed by Fast or Speed commands
   procedure Start_Halt (
         T_Id : in     Train_Id);

	-- Handler for sensor event
	-- Called by Environment Monitor module 
	-- Sensors should be called continously in the direction of travel and no interference whatsoever with train are allowed except through the prescribed functions
   procedure Start_Sensor_Event (
         S_Id    : in     Raildefs.Sensor_Id;
         S_State : in     Raildefs.Sensor_Bit);
		 
		 
	-- Tick 
    -- Internally used
	-- Called by heart beat module provide a clock for system in order to perform task like re-checking, re-acquiring resource or considering resumes
   procedure Start_Tick;


	-- Switching to unmanned mode
	-- Only Speed and Stop command is allowed
   procedure Start_Unmanned (
   T_Id : in     Train_Id);

	-- Switching to manned mode
	-- All operation is allowed
	procedure Start_Manned (
         T_Id : in     Train_Id);



end Controllers;
